package model.dao.registerOrder;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import log.MyLogger;
import model.service.Person;


public class PersonDAOImpl{

	String sql = "" ;
	db.dbconnection dbconnection ;
	Connection myConnection ;
	Statement st ;
	
	public List<Person> getPersonsByLastName(String lastName) {
		List<Person> persons = new ArrayList<Person>();
		
		try {
			

			myConnection = dbconnection.getConnection();
			
			Statement st = myConnection.createStatement();
			String sql = "SELECT * FROM person where person.last_name ILIKE '"+lastName+"%'";
	
			ResultSet resultSet = st.executeQuery(sql);
			
			
			while(resultSet.next()){
				Person user = new Person(); 
				user.setPerson(resultSet.getInt("person"));
				user.setFirstName(resultSet.getString("first_name")); 
				user.setLastName(resultSet.getString("last_name")); 
				user.setIdentityCode(resultSet.getString("identity_code"));
				user.setBirthDate(resultSet.getDate("birth_date"));
				persons.add(user);
			}
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("PersonDAOImpl.getPersonsByLastName()", e.getMessage());
		}
		
		return persons;
	}

	public Person getPersonById(int personID) {
		Person p = new Person();
		p.setPerson(personID);
		try {
			myConnection = dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			String sql = "Select * from person where person="+personID;
			ResultSet resultSet = st.executeQuery(sql);
			
			while(resultSet.next()){
				p.setFirstName(resultSet.getString("first_name"));
				p.setLastName(resultSet.getString("last_name"));
				p.setIdentityCode(resultSet.getString("identity_code"));
				p.setBirthDate(resultSet.getDate("birth_date"));
				
			}
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("PersonDAOImpl.getPersonsById()", e.getMessage());
		}
		return p;
	}

}
